package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.content.mapper.CourseCategoryMapper;
import com.xuecheng.content.model.dto.CourseCategoryTreeDto;
import com.xuecheng.content.model.po.CourseCategory;
import com.xuecheng.content.service.CourseCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author :chenjie
 * @date :Created 2023/2/2 16:20
 */
@Service
public class CourseCategoryServiceImpl extends ServiceImpl<CourseCategoryMapper, CourseCategory> implements CourseCategoryService {
    @Autowired
    CourseCategoryMapper courseCategoryMapper;
    @Override
    public List<CourseCategoryTreeDto> querTreeNodes(String id) {
        //得到根节点下的所以节点
        List<CourseCategoryTreeDto> CategoryTreeDtos = courseCategoryMapper.selectTreeNodes(id);
        List<CourseCategoryTreeDto> courseCategoryTreeDtos=new ArrayList<>();
        HashMap<String,CourseCategoryTreeDto> hashMap=new HashMap<>();
        CategoryTreeDtos.stream().forEach(item->{//利用流处理数据
            hashMap.put(item.getId(),item);
            if(item.getParentid().equals(id)){
                courseCategoryTreeDtos.add(item);
            }
            String parentid = item.getParentid();
            CourseCategoryTreeDto parentNode=hashMap.get(parentid);
            if(parentNode!=null){
                List childrenTreeNode =parentNode.getChildrenTreeNodes();
                if(childrenTreeNode==null){
                    parentNode.setChildrenTreeNodes(new ArrayList<CourseCategoryTreeDto>());
                }
                parentNode.getChildrenTreeNodes().add(item);
            }
        });
        return courseCategoryTreeDtos;
    }
}
